/***************************************************************************
 *            menu.c
 *
 *  Thu Oct  9 08:03:11 2008
 *  Copyright  2008  Tiago Falcao
 *  <developer@tiagofalcao.com>
 ****************************************************************************/

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301,  USA
 */
 
#include "menu.h"
#include "screen.h"

void menu_create(){
	menu_screen=menu_screen_create();
	menu_object=menu_object_create();
	menu_set(menu_screen);
}

void menu_delete(){
	index_string_delete(menu_objs_wavefront);
	index_string_delete(menu_textures);
}

void menu_set(int menu){
	glutSetMenu(menu);
	glutAttachMenu(GLUT_RIGHT_BUTTON);
}


int menu_screen_create(){
	int draw=menu_draw_create();
	int menu=glutCreateMenu(menu_screen_listener);
	glutAddMenuEntry("Select(F3)", MENU_OBJ_SELECT);
	glutAddSubMenu("Draw", draw);
	glutAddMenuEntry("Quit(Q)", MENU_QUIT);
	return menu;
}
	 
void menu_screen_listener(int value){
	switch(value){
		case MENU_OBJ_SELECT:{
			objects_manager_select(objs,0);
			break;
		}
		case MENU_QUIT:{
			end_program();
			break;
		}
	}
}
	 

int menu_draw_create(){
	int obj=menu_draw_obj_create();
	int glut=menu_draw_glut_create();
	int menu=glutCreateMenu(NULL);
	glutAddSubMenu("GLUT", glut);
	glutAddSubMenu("Wavefront OBJ", obj);
	return menu;
}

int menu_draw_obj_create(){
	int menu=glutCreateMenu(menu_draw_obj_listener);
	int i=0;
	menu_objs_wavefront=directory_to_index("./models_obj");
	while(i<menu_objs_wavefront->size){
		glutAddMenuEntry(menu_objs_wavefront->index[i],i);
		printf("%d %d %s\n",menu,i,menu_objs_wavefront->index[i]);
		i++;
	}
	return menu;
}

void menu_draw_obj_listener(int value){
	object * obj;
	char file[100]="./models_obj/";
	strcat(file,index_string_get(menu_objs_wavefront,value));
	printf("Open: %s\n",file);
	obj=objects_manager_add(objs,obj_model_display,obj_model_load(file),obj_model_delete);
    obj->mode=OBJMODEL_MODE_SMOOTH | OBJMODEL_MODE_FILL;
    object_set_color(obj,1.,1.,1.,1.);
    object_set_ambient(obj,.1,.1,.1,1.);
    object_set_diffuse(obj,1.,1.,1.,1.);
    object_set_specular(obj,1.,1.,1.,1.);
    object_set_emission(obj,.0,.0,.0,1.);
    object_set_shininess(obj,100.);
    //Redisplay
    glutPostRedisplay();
}


int menu_draw_glut_create(){
	int menu=glutCreateMenu(menu_draw_glut_listener);
	glutAddMenuEntry("Cube", MENU_DRAW_GLUT_CUBE);
	glutAddMenuEntry("Sphere", MENU_DRAW_GLUT_SPHERE);
	glutAddMenuEntry("Teapot", MENU_DRAW_GLUT_TEAPOT);
	return menu;
}

void menu_draw_glut_listener(int value){
	object * obj;
	switch(value){
		case MENU_DRAW_GLUT_CUBE: //Cubo com glut
			obj=objects_manager_add(objs,obj_simple_cube_display,calloc(1,sizeof(GLfloat)),free);
			*((GLfloat*)obj->object)=.3;
			break;
		case MENU_DRAW_GLUT_SPHERE: //Esfera com glut
			obj=objects_manager_add(objs,obj_simple_sphere_display,calloc(1,sizeof(obj_simple_sphere)),free);
			((obj_simple_sphere*)obj->object)->radius=.15;
			((obj_simple_sphere*)obj->object)->slices=30;
			((obj_simple_sphere*)obj->object)->stacks=30;
			break;
		case MENU_DRAW_GLUT_TEAPOT: //Chaleira com glut
			obj=objects_manager_add(objs,obj_simple_teapot_display,calloc(1,sizeof(GLfloat)),free);
			*((GLfloat*)obj->object)=.3;
			break;
	}
	//Definicoes padroes do objeto criado...um pouco de preguica
	object_set_color(obj,1.,1.,1.,1.);
	object_set_ambient(obj,.1,.1,.1,1.);
	object_set_diffuse(obj,1.,1.,1.,1.);
	object_set_specular(obj,1.,1.,1.,1.);
	object_set_emission(obj,.0,.0,.0,1.);
	object_set_shininess(obj,100.);
	//Redisplay
	glutPostRedisplay();
}





int menu_object_create(){
	int color=menu_color_create();
	int texture=menu_texture_create();
	int mode=menu_mode_create();
	int menu=glutCreateMenu(menu_object_listener);
	glutAddSubMenu("Color", color);
	glutAddSubMenu("Texture", texture);
	glutAddSubMenu("Mode", mode);
	glutAddMenuEntry("Preview(F2)", MENU_OBJ_SELECT_PREV);
	glutAddMenuEntry("Next(F3)", MENU_OBJ_SELECT);
	glutAddMenuEntry("Unselect(ESQ)", MENU_OBJ_UNSELECT);
	glutAddMenuEntry("Delete(END)", MENU_OBJ_DELETE);
	return menu;
}

void menu_object_listener(int value){
	switch(value){
		case MENU_OBJ_SELECT:objects_manager_select_next(objs);break;
		case MENU_OBJ_SELECT_PREV:objects_manager_select_prev(objs);break;
		case MENU_OBJ_UNSELECT:objects_manager_unselect(objs);break;
		case MENU_OBJ_DELETE:objects_manager_delete_selected(objs);break;
	}
}

int menu_color_create(){
	int menu=glutCreateMenu(menu_color_listener);
	glutAddMenuEntry("White", MENU_COLOR_WHITE);
	glutAddMenuEntry("Red", MENU_COLOR_RED);
	glutAddMenuEntry("Green", MENU_COLOR_GREEN);
	glutAddMenuEntry("Blue", MENU_COLOR_BLUE);
	glutAddMenuEntry("Gray", MENU_COLOR_GRAY);
	return menu;
}

void menu_color_listener(int value){
	switch(value){
		case MENU_COLOR_WHITE:object_set_color(objs->selected,1.,1.,1.,1.);break;
		case MENU_COLOR_RED:object_set_color(objs->selected,1.,0.,0.,1.);break;
		case MENU_COLOR_GREEN:object_set_color(objs->selected,.0,1.,0.,1.);break;
		case MENU_COLOR_BLUE:object_set_color(objs->selected,.0,0.,1.,1.);break;
		case MENU_COLOR_GRAY:object_set_color(objs->selected,.7,.7,.7,1.);break;
	}
}

int menu_texture_create(){
        int menu=glutCreateMenu(menu_texture_listener);
        int i=0;
		glutAddMenuEntry("None",-1);
        menu_textures=directory_to_index("./textures");
        while(i<menu_textures->size){
                glutAddMenuEntry(menu_textures->index[i],i);
		printf("%d %d %s\n",menu,i,menu_textures->index[i]);
			i++;
		}
        return menu;
}

void menu_texture_listener(int value){
	if(value==-1){
		objs->selected->texture=0;
	}else{
		char file[100]="./textures/";
		strcat(file,index_string_get(menu_textures,value));
		printf("Open: %s\n",file);
		objs->selected->texture=SOIL_load_OGL_texture(file,SOIL_LOAD_AUTO,SOIL_CREATE_NEW_ID,
		            SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS |  SOIL_FLAG_TEXTURE_REPEATS 
													  | SOIL_FLAG_COMPRESS_TO_DXT);
		printf("Texture: %d\n",objs->selected->texture);
	}
	//Redisplay
	glutPostRedisplay();
}


int menu_mode_create(){
	int menu=glutCreateMenu(menu_mode_listener);
	glutAddMenuEntry("Wire", OBJ3D_MODE_WIRE);
	glutAddMenuEntry("Wire + Fill", OBJ3D_MODE_WIRE | OBJ3D_MODE_FILL);
	glutAddMenuEntry("Flat Fill", OBJ3D_MODE_FILL);
	glutAddMenuEntry("Smooth Fill", OBJ3D_MODE_SMOOTH | OBJ3D_MODE_FILL);
	glutAddMenuEntry("Face", OBJ3D_MODE_FACE);
	glutAddMenuEntry("Face + Fill", OBJ3D_MODE_SMOOTH | OBJ3D_MODE_FACE | OBJ3D_MODE_FILL);
	return menu;
}

void menu_mode_listener(int value){
	objs->selected->mode=OBJ3D_MODE_SELECTED|value;
}


